package com.antis.olsl.magic.report.weekly.detail;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.antis.olsl.arouterlib.MagicRoutePath;
import com.antis.olsl.library.MyApplication;
import com.antis.olsl.base.BaseActivity;
import com.antis.olsl.library.dialog.ChooseSellerDialog;
import com.antis.olsl.library.response.GetEmployeeInfoListResp;
import com.antis.olsl.base.utils.ToastUtil;
import com.antis.olsl.magic.databinding.ActivityWeeklyDetailBinding;
import com.antis.olsl.magic.response.GetSalesSummaryResp;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import timber.log.Timber;

@Route(path = MagicRoutePath.ACTIVITY_WEEKLY_DETAIL)
public class WeeklyDetailActivity extends BaseActivity<ActivityWeeklyDetailBinding> implements WeeklyDetailContract.View, ChooseSellerDialog.ChooseSellerListener {

    @Autowired
    String salesRoomId;
    @Autowired
    int searchDate;
    @Autowired
    int searchWeek;
    @Autowired
    String salesRoomName;

    private String mEmpCode;
    private WeeklyDetailPresenter mPresenter;

    private ChooseSellerDialog mDialog;
    private ArrayList<GetEmployeeInfoListResp.ContentBean> sellerList = new ArrayList<>();
    private ArrayList<GetEmployeeInfoListResp.ContentBean> chooseSellerList = new ArrayList<>();
    private boolean chooseAllSalesroom;

    @Override
    protected ActivityWeeklyDetailBinding getViewBinding() {
        return ActivityWeeklyDetailBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initPresenter() {
        super.initPresenter();
        mPresenter = new WeeklyDetailPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void initViews() {
        viewBinding.rlChooseSeller.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> map = new HashMap<>(2);
                String merchantId = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getMerchantId()) ? MyApplication.getUserInfo().getMerchantId() : "";
                map.put("shopId", salesRoomId);
                map.put("merchantId", merchantId);
                mPresenter.getEmployeeInfoList(map);
            }
        });
    }

    @Override
    protected void initData() throws IllegalAccessException {
        String storeName;
        if (TextUtils.isEmpty(salesRoomName)) {
            storeName = "全部门店";
        } else {
            storeName = salesRoomName;
        }
        viewBinding.textStoreName.setText(storeName);
        String showDate = searchDate + "年" + searchWeek + "周";
        viewBinding.textDate.setText(showDate);

        loadData();
    }

    private void loadData() {
        Map<String, Object> map = new HashMap<>();
        map.put("shopId", salesRoomId);
        map.put("searchTime", searchDate);
        map.put("weekNum", searchWeek);
        map.put("empCode", mEmpCode);
        mPresenter.getWeekSalesDetails(map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.unSubscribe();
        }

        if (null != mDialog) {
            mDialog.dismiss();
        }
    }

    @Override
    public void getWeekSalesDetailsSuccess(GetSalesSummaryResp.ContentBean bean) {
        if (null != bean) {
            float weeklyProgress = 0f;
            if (!TextUtils.isEmpty(bean.zmbdcl)) {
                try {
                    weeklyProgress = Float.parseFloat(bean.zmbdcl);
                } catch (NumberFormatException e) {
                    Timber.tag("hhh").e("exception= " + e);
                }
            }
            viewBinding.weeklySeekBar.setProgress(weeklyProgress);
            float monthlyProgress = 0;
            if (!TextUtils.isEmpty(bean.ymbdcl)) {
                try {
                    monthlyProgress = Float.parseFloat(bean.ymbdcl);
                } catch (NumberFormatException e) {
                    Timber.tag("hhh").e("exception= " + e);
                }
            }
            viewBinding.monthlySeekBar.setProgress(monthlyProgress);

            // 销售金额
            setSalesAmountValue(bean);

            // 销售数量
            setSalesVolumeValue(bean);

            // 会员销售金额
            setMemberAmountValue(bean);

            // 会员销售数量
            setMemberVolumeValue(bean);

            // 连带率
            setJointRateValue(bean);

            // 客单价
            setCustomerPriceValue(bean);

            // 明星单品
            setStarSingleProductValue(bean);

            // 支单价
            setUnitPriceValue(bean);
        }
    }

    private void setSalesAmountValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.xsje)) {
            viewBinding.layoutSalesAmount.textSalesAmount.setText(bean.xsje);
        } else {
            viewBinding.layoutSalesAmount.textSalesAmount.setText("0");
        }

        if (!TextUtils.isEmpty(bean.xsjezb)) {
            String salesProportion = bean.xsjezb + "%";
            viewBinding.layoutSalesAmount.textSalesProportion.setText(salesProportion);
        } else {
            viewBinding.layoutSalesAmount.textSalesProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hbxsje)) {
            String salesMom = bean.hbxsje + "%";
            viewBinding.layoutSalesAmount.textSalesMom.setText(salesMom);
        } else {
            viewBinding.layoutSalesAmount.textSalesMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbxsje)) {
            String salesYoy = bean.tbxsje + "%";
            viewBinding.layoutSalesAmount.textSalesYoy.setText(salesYoy);
        } else {
            viewBinding.layoutSalesAmount.textSalesYoy.setText("-");
        }
    }

    private void setSalesVolumeValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.xssl)) {
            viewBinding.layoutSalesVolume.textSalesVolume.setText(bean.xssl);
        } else {
            viewBinding.layoutSalesVolume.textSalesVolume.setText("0");
        }

        if (!TextUtils.isEmpty(bean.xsslzb)) {
            String volumeProportion = bean.xsslzb + "%";
            viewBinding.layoutSalesVolume.textVolumeProportion.setText(volumeProportion);
        } else {
            viewBinding.layoutSalesVolume.textVolumeProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hb_xssl)) {
            String volumeMom = bean.hb_xssl + "%";
            viewBinding.layoutSalesVolume.textVolumeMom.setText(volumeMom);
        } else {
            viewBinding.layoutSalesVolume.textVolumeMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbxssl)) {
            String volumeYoy = bean.tbxssl + "%";
            viewBinding.layoutSalesVolume.textVolumeYoy.setText(volumeYoy);
        } else {
            viewBinding.layoutSalesVolume.textVolumeYoy.setText("-");
        }
    }

    private void setMemberAmountValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.hyxsje)) {
            viewBinding.layoutMemberSalesAmount.textMemberSalesAmount.setText(bean.hyxsje);
        } else {
            viewBinding.layoutMemberSalesAmount.textMemberSalesAmount.setText("0");
        }

        if (!TextUtils.isEmpty(bean.hyxsjezb)) {
            String memberSalesProportion = bean.hyxsjezb + "%";
            viewBinding.layoutMemberSalesAmount.textMemberSalesProportion.setText(memberSalesProportion);
        } else {
            viewBinding.layoutMemberSalesAmount.textMemberSalesProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hbhyxsje)) {
            String memberSalesMom = bean.hbhyxsje + "%";
            viewBinding.layoutMemberSalesAmount.textMemberSalesMom.setText(memberSalesMom);
        } else {
            viewBinding.layoutMemberSalesAmount.textMemberSalesMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbhyxsje)) {
            String memberSalesYoy = bean.tbhyxsje + "%";
            viewBinding.layoutMemberSalesAmount.textMemberSalesYoy.setText(memberSalesYoy);
        } else {
            viewBinding.layoutMemberSalesAmount.textMemberSalesYoy.setText("-");
        }
    }

    private void setMemberVolumeValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.hyxssl)) {
            viewBinding.layoutMemberSalesVolume.textMemberSalesVolume.setText(bean.hyxssl);
        } else {
            viewBinding.layoutMemberSalesVolume.textMemberSalesVolume.setText("0");
        }

        if (!TextUtils.isEmpty(bean.hyxsslzb)) {
            String memberVolumeProportion = bean.hyxsslzb + "%";
            viewBinding.layoutMemberSalesVolume.textMemberVolumeProportion.setText(memberVolumeProportion);
        } else {
            viewBinding.layoutMemberSalesVolume.textMemberVolumeProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hbhyxssl)) {
            String memberVolumeMom = bean.hbhyxssl + "%";
            viewBinding.layoutMemberSalesVolume.textMemberVolumeMom.setText(memberVolumeMom);
        } else {
            viewBinding.layoutMemberSalesVolume.textMemberVolumeMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbhyxssl)) {
            String memberVolumeYoy = bean.tbhyxssl + "%";
            viewBinding.layoutMemberSalesVolume.textMemberVolumeYoy.setText(memberVolumeYoy);
        } else {
            viewBinding.layoutMemberSalesVolume.textMemberVolumeYoy.setText("-");
        }
    }

    private void setJointRateValue(GetSalesSummaryResp.ContentBean bean) {
        viewBinding.layoutJointRate.textJointRate.setText(bean.ldl);
        if (!TextUtils.isEmpty(bean.hb_ldl)) {
            String jointRateMom = bean.hb_ldl + "%";
            viewBinding.layoutJointRate.textJointRateMom.setText(jointRateMom);
        } else {
            viewBinding.layoutJointRate.textJointRateMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbldl)) {
            String jointRateYoy = bean.tbldl + "%";
            viewBinding.layoutJointRate.textJointRateYoy.setText(jointRateYoy);
        } else {
            viewBinding.layoutJointRate.textJointRateYoy.setText("-");
        }
    }

    private void setCustomerPriceValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.kdj)) {
            viewBinding.layoutCustomerPrice.textCustomerPrice.setText(bean.kdj);
        } else {
            viewBinding.layoutCustomerPrice.textCustomerPrice.setText("0");
        }

        if (!TextUtils.isEmpty(bean.hb_kdj)) {
            String customerPriceMom = bean.hb_kdj + "%";
            viewBinding.layoutCustomerPrice.textCustomerPriceMom.setText(customerPriceMom);
        } else {
            viewBinding.layoutCustomerPrice.textCustomerPriceMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbkdj)) {
            String customerPriceYoy = bean.tbkdj + "%";
            viewBinding.layoutCustomerPrice.textCustomerPriceYoy.setText(customerPriceYoy);
        } else {
            viewBinding.layoutCustomerPrice.textCustomerPriceYoy.setText("-");
        }
    }

    private void setStarSingleProductValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.starpronumber)) {
            viewBinding.layoutStarSingleProduct.textStarSingleProductVolume.setText(bean.starpronumber);
        } else {
            viewBinding.layoutStarSingleProduct.textStarSingleProductVolume.setText("0");
        }

        if (!TextUtils.isEmpty(bean.daliytargetratio)) {
            viewBinding.layoutStarSingleProduct.textDayReach.setText(bean.daliytargetratio);
        } else {
            viewBinding.layoutStarSingleProduct.textDayReach.setText("-");
        }

        if (!TextUtils.isEmpty(bean.ztargetratio)) {
            viewBinding.layoutStarSingleProduct.textWeekReach.setText(bean.ztargetratio);
        } else {
            viewBinding.layoutStarSingleProduct.textWeekReach.setText("-");
        }
    }

    private void setUnitPriceValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.zdj)) {
            viewBinding.layoutUnitPrice.textUnitPrice.setText(bean.zdj);
        } else {
            viewBinding.layoutUnitPrice.textUnitPrice.setText("0");
        }
    }

    @Override
    public void getWeekSalesSummaryEmpty() {
        Timber.tag("hhh").e("getWeekSalesSummaryEmpty");
        setDefaultValue();
    }

    private void setDefaultValue() {
        viewBinding.weeklySeekBar.setProgress(0);
        viewBinding.monthlySeekBar.setProgress(0);
        viewBinding.layoutSalesAmount.textSalesAmount.setText("0");
        viewBinding.layoutSalesAmount.textSalesProportion.setText("-");
        viewBinding.layoutSalesAmount.textSalesMom.setText("-");
        viewBinding.layoutSalesAmount.textSalesYoy.setText("-");
        viewBinding.layoutSalesVolume.textSalesVolume.setText("0");
        viewBinding.layoutSalesVolume.textVolumeProportion.setText("-");
        viewBinding.layoutSalesVolume.textVolumeMom.setText("-");
        viewBinding.layoutSalesVolume.textVolumeYoy.setText("-");
        viewBinding.layoutMemberSalesAmount.textMemberSalesAmount.setText("0");
        viewBinding.layoutMemberSalesAmount.textMemberSalesProportion.setText("-");
        viewBinding.layoutMemberSalesAmount.textMemberSalesMom.setText("-");
        viewBinding.layoutMemberSalesAmount.textMemberSalesYoy.setText("-");
        viewBinding.layoutMemberSalesVolume.textMemberSalesVolume.setText("0");
        viewBinding.layoutMemberSalesVolume.textMemberVolumeProportion.setText("-");
        viewBinding.layoutMemberSalesVolume.textMemberVolumeMom.setText("-");
        viewBinding.layoutMemberSalesVolume.textMemberVolumeYoy.setText("-");
        viewBinding.layoutJointRate.textJointRate.setText("-");
        viewBinding.layoutJointRate.textJointRateMom.setText("-");
        viewBinding.layoutJointRate.textJointRateYoy.setText("-");
        viewBinding.layoutCustomerPrice.textCustomerPrice.setText("0");
        viewBinding.layoutCustomerPrice.textCustomerPriceMom.setText("-");
        viewBinding.layoutCustomerPrice.textCustomerPriceYoy.setText("-");
        viewBinding.layoutStarSingleProduct.textStarSingleProductVolume.setText("0");
        viewBinding.layoutStarSingleProduct.textDayReach.setText("-");
        viewBinding.layoutStarSingleProduct.textWeekReach.setText("-");
        viewBinding.layoutUnitPrice.textUnitPrice.setText("0");
    }

    @Override
    public void getWeekSalesDetailsFailure(String msg) {
        Timber.tag("hhh").e("getWeekSalesDetailsFailure msg= " + msg);
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(this, msg);
        setDefaultValue();
    }

    @Override
    public void getEmployeeInfoListSuccess(List<GetEmployeeInfoListResp.ContentBean> list) {
        sellerList.clear();
        sellerList.addAll(list);

        Timber.tag("hhh").e("getEmployeeInfoListSuccess");
        mDialog = new ChooseSellerDialog(this, sellerList, WeeklyDetailActivity.this);
    }

    @Override
    public void getEmployeeInfoListFailure(String msg) {
        Timber.tag("hhh").e("getEmployeeInfoListFailure");
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(this, msg);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }

    @Override
    public void onChooseSellerListener(boolean isChooseAll, ArrayList<GetEmployeeInfoListResp.ContentBean> choosedFiledInfos, ArrayList<GetEmployeeInfoListResp.ContentBean> sourceInofs, int selectedIndex) {
        chooseAllSalesroom = isChooseAll;
        chooseSellerList = choosedFiledInfos;
        sellerList = sourceInofs;
        if (selectedIndex == -1) {
            salesRoomId = "";
            viewBinding.textSeller.setText("全部销售员");
        } else {
            salesRoomId = filterIds();
        }

        loadData();
    }

    public String filterIds() {
        ArrayList<GetEmployeeInfoListResp.ContentBean> infos = (chooseAllSalesroom ? sellerList : chooseSellerList);
        String name = "";
        String ids = "";
        for (int i = 0; i < infos.size(); i++) {
            ids += TextUtils.isEmpty(ids) ? infos.get(i).id : "," + infos.get(i).id;
            if (chooseAllSalesroom) {
                continue;
            }
            name += TextUtils.isEmpty(name) ? infos.get(i).empName : "," + infos.get(i).empName;
        }
        boolean chooseALL = false;
        if (chooseAllSalesroom) {
            name = "全部门店";
            chooseALL = true;
        }

        if (null != infos.get(0)) {
            mEmpCode = infos.get(0).empCode;
            viewBinding.textSeller.setText(infos.get(0).empName);
        }
        return ids;
    }
}
